<template>
  <section v-loading="loading" class="cms-body">
    <div class="cms-list-header flex-between">
      <div class="el-checkbox-group cms-left">
        <el-button type="primary" icon="el-icon-plus" @click="routerLink('/integralGoods/save','save',0)"
                   v-perms="'/integralGoods/save'">添加
        </el-button>
      </div>
      <!-- 右操作 -->
      <div style="flex:auto" class="content-flex-right">
        <el-select v-model="params.exchangeHot" @change="goQuery">
          <el-option label="热门兑换" value=""></el-option>
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="2"></el-option>
        </el-select>
        <el-select @change="selectTypeChange" clearable placeholder="商品类型" v-model="params.typeName">
          <el-option v-for="item in typeList" :key="item.goodsTypeId" :label="item.typeName" :value="item.goodsTypeId">
          </el-option>
        </el-select>

        <el-select @change="selectTagChange" clearable placeholder="商品分类" v-model="params.tagName">
          <el-option v-for="item in tagList" :key="item.goodsTagId" :label="item.tagName" :value="item.goodsTagId">
          </el-option>
        </el-select>
        <cms-input label="商品名称" v-model="params.goodsName"></cms-input>
        <el-input-number v-model="params.startValue" :min="0" label="初始值" style="width: 125px; line-height: 30px;"></el-input-number>
        <el-input-number v-model="params.endValue" :min="0" label="结束值" style="width: 125px; line-height: 30px;"></el-input-number>
        <el-button @click="goQuery()">查询</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="checkIds">
      <el-table-column type="selection" width="35" align="center"></el-table-column>
      <el-table-column prop="id" label="id" width="130" align="center"></el-table-column>
      <el-table-column prop="tagName" label="商品分类" width="75" align="center"></el-table-column>
      <el-table-column prop="typeName" label="商品类型" width="75" align="center"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称" width="180" align="left"></el-table-column>
      <el-table-column prop="goodsImg" label="封面图" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.goodsImg" min-width="60" height="60">
        </template>
      </el-table-column>
      <el-table-column prop="integralValue" label="积分值" width="60" align="center"></el-table-column>
      <el-table-column prop="exchangeHot" label="热门兑换" width="75" align="center">
        <div slot-scope="scope">
          <el-switch v-model="scope.row.exchangeHot" active-color="#FFA500" inactive-color="#C0CCDA" :active-value="1"
                     :inactive-value="0" @change="changeState(scope.row.id,scope.row.exchangeHot,1)">
          </el-switch>
        </div>
      </el-table-column>
      <el-table-column prop="exchangeHot" label="是否展示" width="75" align="center">
        <div slot-scope="scope">
          <el-switch v-model="scope.row.isDisplay" active-color="#FFA500" inactive-color="#C0CCDA" :active-value="1"
                     :inactive-value="0" @change="changeState(scope.row.id,scope.row.isDisplay,2)">
          </el-switch>
        </div>
      </el-table-column>
      <el-table-column prop="goodsSort" label="排列顺序" align="center" width="75">
        <div slot-scope="scope">
          <el-input v-model="scope.row.goodsSort" type="number" class="w50"></el-input>
        </div>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80">
        <div slot-scope="scope">
          <cms-button type="edit"
                      @click.native="routerLink('/integralGoods/update','update',scope.row.id)"
                      v-perms="'/integralGoods/update'"></cms-button>
          <cms-button type="delete" @click.native="deleteBatch($api.integralGoodsDelete,scope.row.id)"
                      v-perms="'/integralGoods/delete'"></cms-button>
        </div>
      </el-table-column>
    </el-table>
    <!-- 表格底部 -->
    <div class="cms-list-footer">
      <div class="cms-left">
        <el-button :disabled="disabled" @click="batchUpdate($api.integralGoodsBatchUpdate)"
                   v-perms="'/integralGoods/batchUpdate'">保存排序
        </el-button>
        <el-button :disabled="disabled" @click="deleteBatch($api.integralGoodsDelete,ids)"
                   v-perms="'/integralGoods/delete'">批量删除
        </el-button>
      </div>
      <!-- cms分页组件 -->
      <cms-pagination :total="total" @change="getPages"></cms-pagination>
    </div>
  </section>
</template>

<script>
  import listMixins from '@/mixins/list'
  import axios from "axios/index";

  export default {
    mixins: [listMixins],
    data() {
      return {
        params: {//只需要业务参数
          pageNo: 1,
          pageSize: 10,
          exchangeHot: '',
          tagName: '',
          typeName: '',
          goodsTagId: '',
          goodsTypeId: '',
          goodsName: '',
          startValue: '',
          endValue: ''
        },
        tagList: [],
        typeList: [],
      };
    },
    methods: {
      goQuery() {
        this.getTableData(this.params);
      },
      changeState(id, value, type) {
        let params = {}
        if (type == 1) {
          params = {integralGoodsId: id, exchangeHot: value}
        }
        if (type == 2) {
          params = {integralGoodsId: id, isDisplay: value}
        }
        this.$http.post(this.$api.integralGoodsUpdate, params).then(res => {
          if (res.code == "200") {
            this.successMessage('操作成功');
            this.getTableData(this.params);
          }
        });
      },
      getTagAndTypeData() {
        let api = this.$api; //API地址
        axios.all([
          axios.post(api.goodsTagList),
          axios.post(api.goodsTypeList),
        ]).then(axios.spread((tagRes, typeRes) => {
          this.tagList = tagRes.body;
          this.typeList = typeRes.body;
        })).catch(error => {
          console.log(error);
        });
      },
      selectTypeChange(val) {
        this.params.goodsTypeId = val;
        this.getTableData(this.params);
      },
      selectTagChange(val) {
        this.params.goodsTagId = val;
        this.getTableData(this.params);
      },
      batchUpdate(url) { //保存排列循序
        let params = [];
        for (let i in this.checkedList) {
          let param = this.checkedList[i];
          param['integralGoodsId'] = param.id;
          params.push(param)
        }
        params = JSON.stringify(params);
        this.$confirm('是否保存?', '提示', {type: "warning"}).then(mes => {
          this.$http.post(url, {params: params}).then(res => {
            if (res.code == "200") {
              this.successMessage('操作成功');
              this.getTableData(this.params);
            }
          });
        }).catch(error => {
          console.log(error)
        });
      }
    },
    created() {
      this.getTagAndTypeData();
      this.initTableData(this.$api.integralGoodsPageList, this.params);
    }
  };
</script>

<style>

</style>
